package com.punch.common.query;

import com.punch.common.page.PageInfo;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @author tanchangguo
 * @date 2020/7/14 21:16
 */
public class QueryInfo {

    public final static String DESC = "desc";
    public final static String ASC = "asc";

    public final static String DEFAULT_DIRECTION = DESC;
    public final static String DEFAULT_SORT_COLUMN = "ut";
    private static final Map<String, String> replaceColumn = Maps.newHashMap(); //替换查询字段，为了避免暴露数据库字段名

    static {
        replaceColumn.put("ut", "update_time");
    }

    private Integer currPage = PageInfo.DEFAULT_PAGE_NO;
    private Integer pageSize = PageInfo.DEFAULT_PAGE_SIZE;  // TODO: 需要校验分页最大值
    private Map<String, Object> params = Maps.newHashMap();
    private Map<String, String> sort = Maps.newHashMap();
    private String sortStr;

    /**
     * 获取排序字段
     *
     * @return
     */
    public String getOrderBySql() {
        StringBuilder sb = new StringBuilder();
        boolean hasData = false;

        //解析前端传进来的排序字段
        if (StringUtils.isNotBlank(sortStr)) {
            String[] sorts = sortStr.split(",");
            for (String sort : sorts) {
                if (StringUtils.isBlank(sort)) {
                    continue;
                }

                String[] items = sort.split(":");
                if (items.length == 0) {
                    continue;
                }

                String column = items[0];

                sb.append(replaceColumn.getOrDefault(column, column));

                sb.append(" ");
                if (items.length == 2) {
                    String direction = items[1];
                    if (ASC.equalsIgnoreCase(direction) || DESC.equalsIgnoreCase(direction)) {
                        sb.append(direction);
                    } else {
                        sb.append(DEFAULT_DIRECTION);
                    }
                } else {
                    sb.append(DEFAULT_DIRECTION);
                }
                sb.append(",");
                hasData = true;
            }
        }

        //解析后天添加排序字段
        if (sort.size() > 0) {
            for (String key : sort.keySet()) {
                String direction = sort.get(key);

                if (!ASC.equalsIgnoreCase(direction) && !DESC.equalsIgnoreCase(direction)) {
                    direction = DEFAULT_DIRECTION;
                }

                sb.append(replaceColumn.getOrDefault(key, key));
                sb.append(" ");
                sb.append(direction);
                sb.append(",");
            }
            hasData = true;
        }

        if (hasData) {
            sb.deleteCharAt(sb.length() - 1);
        }

        return sb.toString();
    }


    public boolean hasSort() {
        return sort.size() > 0 || StringUtils.isNotBlank(sortStr);
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getOffset() {
        return (getCurrPage() - 1) * getPageSize();
    }


    public Integer getCurrPage() {
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    public void addReplaceColumn(String original, String replaced) {
        replaceColumn.put(original, replaced);
    }

    public void addParams(String name, Object value) {
        params.put(name, value);
    }

    public void addSort(String name, String direction) {
        sort.put(name, direction);
    }

    public void setSortStr(String sortStr) {
        this.sortStr = sortStr;
    }

    public String getSortStr() {
        return this.sortStr;
    }


    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Map<String, Object> params() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
}

